


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>Getting Started Tutorial (Scala): First Chapter &mdash; Akka Documentation</title>
    
    <link rel="stylesheet" href="../_static/style.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    <link rel="stylesheet" href="../_static/prettify.css" type="text/css" />
    <link rel="stylesheet" href="../_static/base.css" type="text/css" />
    <link rel="stylesheet" href="../_static/docs.css" type="text/css" />
    <link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Exo:300,400,600,700" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '2.0.1',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/toc.js"></script>
    <script type="text/javascript" src="../_static/prettify.js"></script>
    <script type="text/javascript" src="../_static/highlightCode.js"></script>
    <script type="text/javascript" src="../_static/effects.core.js"></script>
    <script type="text/javascript" src="../_static/effects.highlight.js"></script>
    <script type="text/javascript" src="../_static/scrollTo.js"></script>
    <script type="text/javascript" src="../_static/contentsFix.js"></script>
    <link rel="shortcut icon" href="../_static/favicon.ico"/>
    <link rel="top" title="Akka Documentation" href="../index.html" />
    <link rel="up" title="Introduction" href="index.html" />
    <link rel="next" title="Getting Started Tutorial (Java): First Chapter" href="getting-started-first-java.html" />
    <link rel="prev" title="Getting Started" href="getting-started.html" /> 
  </head>
  <body>
  <div class="navbar">
    <div class="navbar-inner">
      <div class="container">
        <div class="navbar-logo">
          <a href="http://akka.io"><img src="../_static/logo-small.png" /></a>
        </div>    
        <ul class="nav">
          <li><a href="http://akka.io/docs">Documentation</a></li>
          <li><a href="http://akka.io/downloads">Download</a></li>
          <li><a href="http://groups.google.com/group/akka-user">Mailing List</a></li>
          <li><a href="http://github.com/akka/akka">Code</a></li>           
          <li><a href="http://typesafe.com/products/typesafe-subscription">Commerical Support</a></li>
        </ul>
      </div>
    </div>
  </div>
  <div class="main">
    <div class="container">
      <div class="page-title">Getting Started Tutorial (Scala): First Chapter</div><div class="pdf-link"><a href="http://akka.io/docs/akka/2.0.1/Akka.pdf"><img src="../_static/pdf-icon.png" style="height: 40px;" /></a></div></div>
    <div class="main-container">
      <div class="container">
        <div class="row">
          <div class="span12">
            <ul class="breadcrumb">           
              <li>
                 <span class="divider">|</span> <a href="getting-started-first-java.html">Getting Started Tutorial (Java): First Chapter</a> <span class="divider">»</span>
              </li>
              <li>
                <a href="../index.html">Contents</a>
              </li>
              <li>
                <span class="divider">«</span> <a href="getting-started.html">Getting Started</a> <span class="divider">|</span>
              </li>
              <li>
                Version 2.0.1
              </li>
            </ul>         
          </div>
        </div>
        <div class="row">
          <div class="span9">
            
  <div class="section" id="getting-started-tutorial-scala-first-chapter">
<span id="getting-started-first-scala"></span><h1>Getting Started Tutorial (Scala): First Chapter</h1>
<div class="section" id="introduction">
<h2>Introduction</h2>
<p>Welcome to the first tutorial on how to get started with Akka and Scala. We
assume that you already know what Akka and Scala are and will now focus on the
steps necessary to start your first project.</p>
<p>There are two variations of this first tutorial:</p>
<ul class="simple">
<li>creating a standalone project and run it from the command line</li>
<li>creating a SBT (Simple Build Tool) project and running it from within SBT</li>
</ul>
<p>Since they are so similar we will present them both.</p>
<p>The sample application that we will create is using actors to calculate the
value of Pi. Calculating Pi is a CPU intensive operation and we will utilize
Akka Actors to write a concurrent solution that scales out to multi-core
processors. This sample will be extended in future tutorials to use Akka Remote
Actors to scale out on multiple machines in a cluster.</p>
<p>We will be using an algorithm that is called &#8220;embarrassingly parallel&#8221; which
just means that each job is completely isolated and not coupled with any other
job. Since this algorithm is so parallelizable it suits the actor model very
well.</p>
<p>Here is the formula for the algorithm we will use:</p>
<img alt="../_images/pi-formula.png" src="../_images/pi-formula.png" />
<p>In this particular algorithm the master splits the series into chunks which are
sent out to each worker actor to be processed. When each worker has processed
its chunk it sends a result back to the master which aggregates the total
result.</p>
<div class="section" id="tutorial-source-code">
<h3>Tutorial source code</h3>
<p>If you want don&#8217;t want to type in the code and/or set up an SBT project then you can
check out the full tutorial from the Akka GitHub repository. It is in the
<tt class="docutils literal"><span class="pre">akka-tutorials/akka-tutorial-first</span></tt> module. You can also browse it online
<a class="reference external" href="https://github.com/akka/akka/tree/master/akka-tutorials/akka-tutorial-first">here</a>, with the actual source code <a class="reference external" href="https://github.com/akka/akka/blob/master/akka-tutorials/akka-tutorial-first/src/main/scala/akka/tutorial/first/scala/Pi.scala">here</a>.</p>
<p>To check out the code using Git invoke the following command and you can then you can navigate down to the tutorial.</p>
<p>On Linux/Unix/Mac systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">$</span> <span class="n">git</span> <span class="n">clone</span> <span class="n">git</span><span class="o">://</span><span class="n">github</span><span class="o">.</span><span class="n">com</span><span class="o">/</span><span class="n">akka</span><span class="o">/</span><span class="n">akka</span><span class="o">.</span><span class="n">git</span>
<span class="n">$</span> <span class="n">cd</span> <span class="n">akka</span><span class="o">/</span><span class="n">akka</span><span class="o">-</span><span class="n">tutorials</span><span class="o">/</span><span class="n">akka</span><span class="o">-</span><span class="n">tutorial</span><span class="o">-</span><span class="n">first</span>
</pre></div>
</div>
<p>On Windows systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">&gt;</span> <span class="n">git</span> <span class="n">clone</span> <span class="n">git</span><span class="o">://</span><span class="n">github</span><span class="o">.</span><span class="n">com</span><span class="o">/</span><span class="n">akka</span><span class="o">/</span><span class="n">akka</span><span class="o">.</span><span class="n">git</span>
<span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">&gt;</span> <span class="n">cd</span> <span class="n">akka</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="n">tutorials</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="n">tutorial</span><span class="o">-</span><span class="n">first</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="prerequisites">
<h2>Prerequisites</h2>
<p>This tutorial assumes that you have Java 1.6 or later installed on you machine
and <tt class="docutils literal"><span class="pre">java</span></tt> on your <tt class="docutils literal"><span class="pre">PATH</span></tt>. You also need to know how to run commands in a
shell (ZSH, Bash, DOS etc.) and a decent text editor or IDE to type in the Scala
code.</p>
<p>You need to make sure that <tt class="docutils literal"><span class="pre">$JAVA_HOME</span></tt> environment variable is set to the
root of the Java distribution. You also need to make sure that the
<tt class="docutils literal"><span class="pre">$JAVA_HOME/bin</span></tt> is on your <tt class="docutils literal"><span class="pre">PATH</span></tt>.</p>
<p>On Linux/Unix/Mac systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">$</span> <span class="n">export</span> <span class="nc">JAVA_HOME</span><span class="o">=..</span><span class="n">root</span> <span class="n">of</span> <span class="nc">Java</span> <span class="n">distribution</span><span class="o">..</span>
<span class="n">$</span> <span class="n">export</span> <span class="nc">PATH</span><span class="o">=</span><span class="nc">$PATH</span><span class="k">:</span><span class="kt">$JAVA_HOME/bin</span>
</pre></div>
</div>
<p>You can test your installation by invoking <tt class="docutils literal"><span class="pre">java</span></tt>:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">$</span> <span class="n">java</span> <span class="o">-</span><span class="n">version</span>
<span class="n">java</span> <span class="n">version</span> <span class="s">&quot;1.6.0_24&quot;</span>
<span class="nc">Java</span><span class="o">(</span><span class="nc">TM</span><span class="o">)</span> <span class="nc">SE</span> <span class="nc">Runtime</span> <span class="nc">Environment</span> <span class="o">(</span><span class="n">build</span> <span class="mf">1.6</span><span class="o">.</span><span class="mi">0</span><span class="n">_24</span><span class="o">-</span><span class="n">b07</span><span class="o">-</span><span class="mi">334</span><span class="o">-</span><span class="mi">10</span><span class="n">M3326</span><span class="o">)</span>
<span class="nc">Java</span> <span class="nc">HotSpot</span><span class="o">(</span><span class="nc">TM</span><span class="o">)</span> <span class="mi">64</span><span class="o">-</span><span class="nc">Bit</span> <span class="nc">Server</span> <span class="nc">VM</span> <span class="o">(</span><span class="n">build</span> <span class="mf">19.1</span><span class="o">-</span><span class="n">b02</span><span class="o">-</span><span class="mi">334</span><span class="o">,</span> <span class="n">mixed</span> <span class="n">mode</span><span class="o">)</span>
</pre></div>
</div>
<p>On Windows systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">&gt;</span> <span class="n">set</span> <span class="nc">JAVA_HOME</span><span class="o">=..</span><span class="n">root</span> <span class="n">of</span> <span class="nc">Java</span> <span class="n">distribution</span><span class="o">..</span>
<span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">&gt;</span> <span class="n">set</span> <span class="nc">PATH</span><span class="o">=%</span><span class="nc">PATH</span><span class="o">%;%</span><span class="nc">JAVA_HOME</span><span class="o">%/</span><span class="n">bin</span>
</pre></div>
</div>
<p>You can test your installation by invoking <tt class="docutils literal"><span class="pre">java</span></tt>:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">&gt;</span> <span class="n">java</span> <span class="o">-</span><span class="n">version</span>
<span class="n">java</span> <span class="n">version</span> <span class="s">&quot;1.6.0_24&quot;</span>
<span class="nc">Java</span><span class="o">(</span><span class="nc">TM</span><span class="o">)</span> <span class="nc">SE</span> <span class="nc">Runtime</span> <span class="nc">Environment</span> <span class="o">(</span><span class="n">build</span> <span class="mf">1.6</span><span class="o">.</span><span class="mi">0</span><span class="n">_24</span><span class="o">-</span><span class="n">b07</span><span class="o">-</span><span class="mi">334</span><span class="o">-</span><span class="mi">10</span><span class="n">M3326</span><span class="o">)</span>
<span class="nc">Java</span> <span class="nc">HotSpot</span><span class="o">(</span><span class="nc">TM</span><span class="o">)</span> <span class="mi">64</span><span class="o">-</span><span class="nc">Bit</span> <span class="nc">Server</span> <span class="nc">VM</span> <span class="o">(</span><span class="n">build</span> <span class="mf">19.1</span><span class="o">-</span><span class="n">b02</span><span class="o">-</span><span class="mi">334</span><span class="o">,</span> <span class="n">mixed</span> <span class="n">mode</span><span class="o">)</span>
</pre></div>
</div>
</div>
<div class="section" id="downloading-and-installing-akka">
<h2>Downloading and installing Akka</h2>
<p>To build and run the tutorial sample from the command line, you have to download
Akka. If you prefer to use SBT to build and run the sample then you can skip this
section and jump to the next one.</p>
<p>Let&#8217;s get the <tt class="docutils literal"><span class="pre">akka-2.0.1.zip</span></tt> distribution of Akka from
<a class="reference external" href="http://akka.io/downloads/">http://akka.io/downloads/</a> which includes everything we need for this
tutorial. Once you have downloaded the distribution unzip it in the folder you
would like to have Akka installed in. In my case I choose to install it in
<tt class="docutils literal"><span class="pre">/Users/jboner/tools/</span></tt>, simply by unzipping it to this directory.</p>
<p>You need to do one more thing in order to install Akka properly: set the
<tt class="docutils literal"><span class="pre">AKKA_HOME</span></tt> environment variable to the root of the distribution. In my case
I&#8217;m opening up a shell, navigating down to the distribution, and setting the
<tt class="docutils literal"><span class="pre">AKKA_HOME</span></tt> variable.</p>
<p>On Linux/Unix/Mac systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">$</span> <span class="n">cd</span> <span class="o">/</span><span class="nc">Users</span><span class="o">/</span><span class="n">jboner</span><span class="o">/</span><span class="n">tools</span><span class="o">/</span><span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span>
<span class="n">$</span> <span class="n">export</span> <span class="nc">AKKA_HOME</span><span class="o">=</span><span class="n">`pwd`</span>
<span class="n">$</span> <span class="n">echo</span> <span class="nc">$AKKA_HOME</span>
<span class="o">/</span><span class="nc">Users</span><span class="o">/</span><span class="n">jboner</span><span class="o">/</span><span class="n">tools</span><span class="o">/</span><span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span>
</pre></div>
</div>
<p>On Windows systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">&gt;</span> <span class="n">cd</span> <span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span>
<span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span><span class="o">&gt;</span> <span class="n">set</span> <span class="nc">AKKA_HOME</span><span class="o">=%</span><span class="n">cd</span><span class="o">%</span>
<span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span><span class="o">&gt;</span> <span class="n">echo</span> <span class="o">%</span><span class="nc">AKKA_HOME</span><span class="o">%</span>
<span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span>
</pre></div>
</div>
<p>The distribution looks like this.</p>
<p>On Linux/Unix/Mac systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">$</span> <span class="n">ls</span> <span class="o">-</span><span class="mi">1</span>
<span class="n">bin</span>
<span class="n">config</span>
<span class="n">deploy</span>
<span class="n">doc</span>
<span class="n">lib</span>
<span class="n">src</span>
</pre></div>
</div>
<p>On Windows systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span><span class="o">&gt;</span> <span class="n">dir</span>
<span class="n">bin</span>
<span class="n">config</span>
<span class="n">deploy</span>
<span class="n">doc</span>
<span class="n">lib</span>
<span class="n">src</span>
</pre></div>
</div>
<ul class="simple">
<li>In the <tt class="docutils literal"><span class="pre">bin</span></tt> directory we have scripts for starting the Akka Microkernel.</li>
<li>In the <tt class="docutils literal"><span class="pre">config</span></tt> directory we have the Akka conf files.</li>
<li>In the <tt class="docutils literal"><span class="pre">deploy</span></tt> directory we can place applications to be run with the microkernel.</li>
<li>In the <tt class="docutils literal"><span class="pre">doc</span></tt> directory we have the documentation, API, and doc JARs.</li>
<li>In the <tt class="docutils literal"><span class="pre">lib</span></tt> directory we have the Scala and Akka JARs.</li>
<li>In the <tt class="docutils literal"><span class="pre">src</span></tt> directory we have the source JARs for Akka.</li>
</ul>
<p>The only JAR we will need for this tutorial (apart from the
<tt class="docutils literal"><span class="pre">scala-library.jar</span></tt> JAR) is the <tt class="docutils literal"><span class="pre">akka-actor-2.0.1.jar</span></tt> JAR in the <tt class="docutils literal"><span class="pre">lib/akka</span></tt>
directory. This is a self-contained JAR with zero dependencies and contains
everything we need to write a system using Actors.</p>
<p>Akka is very modular and has many JARs for containing different features. The
modules are:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">akka-actor</span></tt> &#8211; Actors</li>
<li><tt class="docutils literal"><span class="pre">akka-remote</span></tt> &#8211; Remote Actors</li>
<li><tt class="docutils literal"><span class="pre">akka-slf4j</span></tt> &#8211; SLF4J Event Handler Listener for logging with SLF4J</li>
<li><tt class="docutils literal"><span class="pre">akka-testkit</span></tt> &#8211; Toolkit for testing Actors</li>
<li><tt class="docutils literal"><span class="pre">akka-kernel</span></tt> &#8211; Akka microkernel for running a bare-bones mini application server</li>
<li><tt class="docutils literal"><span class="pre">akka-durable-mailboxes</span></tt> &#8211; Durable mailboxes: file-based, MongoDB, Redis, Zookeeper</li>
<li><tt class="docutils literal"><span class="pre">akka-amqp</span></tt> &#8211; AMQP integration</li>
</ul>
</div>
<div class="section" id="downloading-and-installing-scala">
<h2>Downloading and installing Scala</h2>
<p>To build and run the tutorial sample from the command line, you have to install
the Scala distribution. If you prefer to use SBT to build and run the sample
then you can skip this section and jump to the next one.</p>
<p>Scala can be downloaded from <a class="reference external" href="http://www.scala-lang.org/downloads">http://www.scala-lang.org/downloads</a>. Browse there
and download the Scala 2.9.1 release. If you pick the <tt class="docutils literal"><span class="pre">tgz</span></tt> or <tt class="docutils literal"><span class="pre">zip</span></tt>
distribution then just unzip it where you want it installed. If you pick the
IzPack Installer then double click on it and follow the instructions.</p>
<p>You also need to make sure that the <tt class="docutils literal"><span class="pre">scala-2.9.1/bin</span></tt> (if that is the
directory where you installed Scala) is on your <tt class="docutils literal"><span class="pre">PATH</span></tt>.</p>
<p>On Linux/Unix/Mac systems:</p>
<div class="highlight-scala"><pre>$ export PATH=$PATH:scala-2.9.1/bin</pre>
</div>
<p>On Windows systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span><span class="o">&gt;</span> <span class="n">set</span> <span class="nc">PATH</span><span class="o">=%</span><span class="nc">PATH</span><span class="o">%;</span><span class="n">scala</span><span class="o">-</span><span class="mf">2.9</span><span class="o">.</span><span class="mi">1</span><span class="o">\</span><span class="n">bin</span>
</pre></div>
</div>
<p>You can test your installation by invoking scala.</p>
<p>On Linux/Unix/Mac systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">$</span> <span class="n">scala</span> <span class="o">-</span><span class="n">version</span>
<span class="nc">Scala</span> <span class="n">code</span> <span class="n">runner</span> <span class="n">version</span> <span class="mf">2.9</span><span class="o">.</span><span class="mf">1.f</span><span class="n">inal</span> <span class="o">--</span> <span class="nc">Copyright</span> <span class="mi">2002</span><span class="o">-</span><span class="mi">2011</span><span class="o">,</span> <span class="nc">LAMP</span><span class="o">/</span><span class="nc">EPFL</span>
</pre></div>
</div>
<p>On Windows systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span><span class="o">&gt;</span> <span class="n">scala</span> <span class="o">-</span><span class="n">version</span>
<span class="nc">Scala</span> <span class="n">code</span> <span class="n">runner</span> <span class="n">version</span> <span class="mf">2.9</span><span class="o">.</span><span class="mf">1.f</span><span class="n">inal</span> <span class="o">--</span> <span class="nc">Copyright</span> <span class="mi">2002</span><span class="o">-</span><span class="mi">2011</span><span class="o">,</span> <span class="nc">LAMP</span><span class="o">/</span><span class="nc">EPFL</span>
</pre></div>
</div>
<p>Looks like we are all good. Finally let&#8217;s create a source file <tt class="docutils literal"><span class="pre">Pi.scala</span></tt> for
the tutorial and put it in the root of the Akka distribution in the <tt class="docutils literal"><span class="pre">tutorial</span></tt>
directory (you have to create it first).</p>
<p>Some tools require you to set the <tt class="docutils literal"><span class="pre">SCALA_HOME</span></tt> environment variable to the
root of the Scala distribution, however Akka does not require that.</p>
</div>
<div class="section" id="downloading-and-installing-sbt">
<span id="getting-started-first-scala-download-sbt"></span><h2>Downloading and installing SBT</h2>
<p>SBT, short for &#8216;Simple Build Tool&#8217; is an excellent build system written in
Scala. It uses Scala to write the build scripts which gives you a lot of
power. It has a plugin architecture with many plugins available, something that
we will take advantage of soon. SBT is the preferred way of building software in
Scala and is probably the easiest way of getting through this tutorial. If you
want to use SBT for this tutorial then follow the following instructions, if not
you can skip this section and the next.</p>
<p>To install SBT and create a project for this tutorial it is easiest to follow
the instructions on <a class="reference external" href="https://github.com/harrah/xsbt/wiki/Setup">https://github.com/harrah/xsbt/wiki/Setup</a>.</p>
<p>Now we need to create our first Akka project. You could add the dependencies
manually to the build script, but the easiest way is to use Akka&#8217;s SBT Plugin,
covered in the next section.</p>
</div>
<div class="section" id="creating-an-akka-sbt-project">
<h2>Creating an Akka SBT project</h2>
<p>If you have not already done so, now is the time to create an SBT project for
our tutorial. You do that by adding the following content to <tt class="docutils literal"><span class="pre">build.sbt</span></tt> file
in the directory you want to create your project in:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">name</span> <span class="o">:=</span> <span class="s">&quot;My Project&quot;</span>

<span class="n">version</span> <span class="o">:=</span> <span class="s">&quot;1.0&quot;</span>

<span class="n">scalaVersion</span> <span class="o">:=</span> <span class="s">&quot;2.9.1&quot;</span>

<span class="n">resolvers</span> <span class="o">+=</span> <span class="s">&quot;Typesafe Repository&quot;</span> <span class="n">at</span> <span class="s">&quot;http://repo.typesafe.com/typesafe/releases/&quot;</span>

<span class="n">libraryDependencies</span> <span class="o">+=</span> <span class="s">&quot;com.typesafe.akka&quot;</span> <span class="o">%</span> <span class="s">&quot;akka-actor&quot;</span> <span class="o">%</span> <span class="s">&quot;2.0.1&quot;</span>
</pre></div>
</div>
<p>Create a directory <tt class="docutils literal"><span class="pre">src/main/scala</span></tt> in which you will store the Scala source
files.</p>
<p>Not needed in this tutorial, but if you would like to use additional Akka
modules beyond <tt class="docutils literal"><span class="pre">akka-actor</span></tt>, you can add these as <tt class="docutils literal"><span class="pre">libraryDependencies</span></tt> in
<tt class="docutils literal"><span class="pre">build.sbt</span></tt>. Note that there must be a blank line between each. Here is an
example adding <tt class="docutils literal"><span class="pre">akka-remote</span></tt>:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">libraryDependencies</span> <span class="o">+=</span> <span class="s">&quot;com.typesafe.akka&quot;</span> <span class="o">%</span> <span class="s">&quot;akka-actor&quot;</span> <span class="o">%</span> <span class="s">&quot;2.0.1&quot;</span>

<span class="n">libraryDependencies</span> <span class="o">+=</span> <span class="s">&quot;com.typesafe.akka&quot;</span> <span class="o">%</span> <span class="s">&quot;akka-remote&quot;</span> <span class="o">%</span> <span class="s">&quot;2.0.1&quot;</span>
</pre></div>
</div>
<p>So, now we are all set.</p>
<p>SBT itself needs a whole bunch of dependencies but our project will only need
one; <tt class="docutils literal"><span class="pre">akka-actor-2.0.1.jar</span></tt>. SBT will download that as well.</p>
</div>
<div class="section" id="start-writing-the-code">
<h2>Start writing the code</h2>
<p>Now it&#8217;s about time to start hacking.</p>
<p>We start by creating a <tt class="docutils literal"><span class="pre">Pi.scala</span></tt> file and adding these import statements at
the top of the file:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">import</span> <span class="nn">akka.actor._</span>
<span class="k">import</span> <span class="nn">akka.routing.RoundRobinRouter</span>
<span class="k">import</span> <span class="nn">akka.util.Duration</span>
<span class="k">import</span> <span class="nn">akka.util.duration._</span>
</pre></div>
</div>
<p>If you are using SBT in this tutorial then create the file in the
<tt class="docutils literal"><span class="pre">src/main/scala</span></tt> directory.</p>
<p>If you are using the command line tools then create the file wherever you
want. I will create it in a directory called <tt class="docutils literal"><span class="pre">tutorial</span></tt> at the root of the
Akka distribution, e.g. in <tt class="docutils literal"><span class="pre">$AKKA_HOME/tutorial/Pi.scala</span></tt>.</p>
</div>
<div class="section" id="creating-the-messages">
<h2>Creating the messages</h2>
<p>The design we are aiming for is to have one <tt class="docutils literal"><span class="pre">Master</span></tt> actor initiating the
computation, creating a set of <tt class="docutils literal"><span class="pre">Worker</span></tt> actors. Then it splits up the work
into discrete chunks, and sends these chunks to the different workers in a
round-robin fashion. The master waits until all the workers have completed their
work and sent back results for aggregation. When computation is completed the
master sends the result to the <tt class="docutils literal"><span class="pre">Listener</span></tt>, which prints out the result.</p>
<p>With this in mind, let&#8217;s now create the messages that we want to have flowing in
the system. We need four different messages:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">Calculate</span></tt> &#8211; sent to the <tt class="docutils literal"><span class="pre">Master</span></tt> actor to start the calculation</li>
<li><tt class="docutils literal"><span class="pre">Work</span></tt> &#8211; sent from the <tt class="docutils literal"><span class="pre">Master</span></tt> actor to the <tt class="docutils literal"><span class="pre">Worker</span></tt> actors containing
the work assignment</li>
<li><tt class="docutils literal"><span class="pre">Result</span></tt> &#8211; sent from the <tt class="docutils literal"><span class="pre">Worker</span></tt> actors to the <tt class="docutils literal"><span class="pre">Master</span></tt> actor
containing the result from the worker&#8217;s calculation</li>
<li><tt class="docutils literal"><span class="pre">PiApproximation</span></tt> &#8211; sent from the <tt class="docutils literal"><span class="pre">Master</span></tt> actor to the
<tt class="docutils literal"><span class="pre">Listener</span></tt> actor containing the the final pi result and how long time
the calculation took</li>
</ul>
<p>Messages sent to actors should always be immutable to avoid sharing mutable
state. In scala we have &#8216;case classes&#8217; which make excellent messages. So let&#8217;s
start by creating three messages as case classes.  We also create a common base
trait for our messages (that we define as being <tt class="docutils literal"><span class="pre">sealed</span></tt> in order to prevent
creating messages outside our control):</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">sealed</span> <span class="k">trait</span> <span class="nc">PiMessage</span>
<span class="k">case</span> <span class="k">object</span> <span class="nc">Calculate</span> <span class="k">extends</span> <span class="nc">PiMessage</span>
<span class="k">case</span> <span class="k">class</span> <span class="nc">Work</span><span class="o">(</span><span class="n">start</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">nrOfElements</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span> <span class="k">extends</span> <span class="nc">PiMessage</span>
<span class="k">case</span> <span class="k">class</span> <span class="nc">Result</span><span class="o">(</span><span class="n">value</span><span class="k">:</span> <span class="kt">Double</span><span class="o">)</span> <span class="k">extends</span> <span class="nc">PiMessage</span>
<span class="k">case</span> <span class="k">class</span> <span class="nc">PiApproximation</span><span class="o">(</span><span class="n">pi</span><span class="k">:</span> <span class="kt">Double</span><span class="o">,</span> <span class="n">duration</span><span class="k">:</span> <span class="kt">Duration</span><span class="o">)</span>
</pre></div>
</div>
</div>
<div class="section" id="creating-the-worker">
<h2>Creating the worker</h2>
<p>Now we can create the worker actor. This is done by mixing in the <tt class="docutils literal"><span class="pre">Actor</span></tt>
trait and defining the <tt class="docutils literal"><span class="pre">receive</span></tt> method. The <tt class="docutils literal"><span class="pre">receive</span></tt> method defines our
message handler. We expect it to be able to handle the <tt class="docutils literal"><span class="pre">Work</span></tt> message so we
need to add a handler for this message:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Worker</span> <span class="k">extends</span> <span class="nc">Actor</span> <span class="o">{</span>

  <span class="c1">// calculatePiFor ...</span>

  <span class="k">def</span> <span class="n">receive</span> <span class="k">=</span> <span class="o">{</span>
    <span class="k">case</span> <span class="nc">Work</span><span class="o">(</span><span class="n">start</span><span class="o">,</span> <span class="n">nrOfElements</span><span class="o">)</span> <span class="k">⇒</span>
      <span class="n">sender</span> <span class="o">!</span> <span class="nc">Result</span><span class="o">(</span><span class="n">calculatePiFor</span><span class="o">(</span><span class="n">start</span><span class="o">,</span> <span class="n">nrOfElements</span><span class="o">))</span> <span class="c1">// perform the work</span>
  <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<p>As you can see we have now created an <tt class="docutils literal"><span class="pre">Actor</span></tt> with a <tt class="docutils literal"><span class="pre">receive</span></tt> method as a
handler for the <tt class="docutils literal"><span class="pre">Work</span></tt> message. In this handler we invoke the
<tt class="docutils literal"><span class="pre">calculatePiFor(..)</span></tt> method, wrap the result in a <tt class="docutils literal"><span class="pre">Result</span></tt> message and send
it back asynchronously to the original sender using the <tt class="docutils literal"><span class="pre">sender</span></tt> reference.
In Akka the sender reference is implicitly passed along with the message so that
the receiver can always reply or store away the sender reference for future use.</p>
<p>The only thing missing in our <tt class="docutils literal"><span class="pre">Worker</span></tt> actor is the implementation on the
<tt class="docutils literal"><span class="pre">calculatePiFor(..)</span></tt> method. While there are many ways we can implement this
algorithm in Scala, in this introductory tutorial we have chosen an imperative
style using a for comprehension and an accumulator:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">def</span> <span class="n">calculatePiFor</span><span class="o">(</span><span class="n">start</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">nrOfElements</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span><span class="k">:</span> <span class="kt">Double</span> <span class="o">=</span> <span class="o">{</span>
  <span class="k">var</span> <span class="n">acc</span> <span class="k">=</span> <span class="mf">0.0</span>
  <span class="k">for</span> <span class="o">(</span><span class="n">i</span> <span class="k">←</span> <span class="n">start</span> <span class="n">until</span> <span class="o">(</span><span class="n">start</span> <span class="o">+</span> <span class="n">nrOfElements</span><span class="o">))</span>
    <span class="n">acc</span> <span class="o">+=</span> <span class="mf">4.0</span> <span class="o">*</span> <span class="o">(</span><span class="mi">1</span> <span class="o">-</span> <span class="o">(</span><span class="n">i</span> <span class="o">%</span> <span class="mi">2</span><span class="o">)</span> <span class="o">*</span> <span class="mi">2</span><span class="o">)</span> <span class="o">/</span> <span class="o">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="o">)</span>
  <span class="n">acc</span>
<span class="o">}</span>
</pre></div>
</div>
</div>
<div class="section" id="creating-the-master">
<h2>Creating the master</h2>
<p>The master actor is a little bit more involved. In its constructor we create a round-robin router
to make it easier to spread out the work evenly between the workers. Let&#8217;s do that first:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">val</span> <span class="n">workerRouter</span> <span class="k">=</span> <span class="n">context</span><span class="o">.</span><span class="n">actorOf</span><span class="o">(</span>
  <span class="nc">Props</span><span class="o">[</span><span class="kt">Worker</span><span class="o">].</span><span class="n">withRouter</span><span class="o">(</span><span class="nc">RoundRobinRouter</span><span class="o">(</span><span class="n">nrOfWorkers</span><span class="o">)),</span> <span class="n">name</span> <span class="k">=</span> <span class="s">&quot;workerRouter&quot;</span><span class="o">)</span>
</pre></div>
</div>
<p>Now we have a router that is representing all our workers in a single
abstraction. So now let&#8217;s create the master actor. We pass it three integer variables:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">nrOfWorkers</span></tt> &#8211; defining how many workers we should start up</li>
<li><tt class="docutils literal"><span class="pre">nrOfMessages</span></tt> &#8211; defining how many number chunks to send out to the workers</li>
<li><tt class="docutils literal"><span class="pre">nrOfElements</span></tt> &#8211; defining how big the number chunks sent to each worker should be</li>
</ul>
<p>Here is the master actor:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Master</span><span class="o">(</span><span class="n">nrOfWorkers</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">nrOfMessages</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">nrOfElements</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">listener</span><span class="k">:</span> <span class="kt">ActorRef</span><span class="o">)</span>
  <span class="k">extends</span> <span class="nc">Actor</span> <span class="o">{</span>

  <span class="k">var</span> <span class="n">pi</span><span class="k">:</span> <span class="kt">Double</span> <span class="o">=</span> <span class="k">_</span>
  <span class="k">var</span> <span class="n">nrOfResults</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="k">_</span>
  <span class="k">val</span> <span class="n">start</span><span class="k">:</span> <span class="kt">Long</span> <span class="o">=</span> <span class="nc">System</span><span class="o">.</span><span class="n">currentTimeMillis</span>

  <span class="k">val</span> <span class="n">workerRouter</span> <span class="k">=</span> <span class="n">context</span><span class="o">.</span><span class="n">actorOf</span><span class="o">(</span>
    <span class="nc">Props</span><span class="o">[</span><span class="kt">Worker</span><span class="o">].</span><span class="n">withRouter</span><span class="o">(</span><span class="nc">RoundRobinRouter</span><span class="o">(</span><span class="n">nrOfWorkers</span><span class="o">)),</span> <span class="n">name</span> <span class="k">=</span> <span class="s">&quot;workerRouter&quot;</span><span class="o">)</span>

  <span class="k">def</span> <span class="n">receive</span> <span class="k">=</span> <span class="o">{</span>
    <span class="c1">// handle messages ...</span>
  <span class="o">}</span>

<span class="o">}</span>
</pre></div>
</div>
<p>A couple of things are worth explaining further.</p>
<p>Note that we are passing in a <tt class="docutils literal"><span class="pre">ActorRef</span></tt> to the <tt class="docutils literal"><span class="pre">Master</span></tt> actor. This is used to
report the the final result to the outside world.</p>
<p>But we are not done yet. We are missing the message handler for the <tt class="docutils literal"><span class="pre">Master</span></tt>
actor. This message handler needs to be able to react to two different messages:</p>
<ul class="simple">
<li><tt class="docutils literal"><span class="pre">Calculate</span></tt> &#8211; which should start the calculation</li>
<li><tt class="docutils literal"><span class="pre">Result</span></tt> &#8211; which should aggregate the different results</li>
</ul>
<p>The <tt class="docutils literal"><span class="pre">Calculate</span></tt> handler is sending out work to all the <tt class="docutils literal"><span class="pre">Worker</span></tt> via its router.</p>
<p>The <tt class="docutils literal"><span class="pre">Result</span></tt> handler gets the value from the <tt class="docutils literal"><span class="pre">Result</span></tt> message and aggregates it to
our <tt class="docutils literal"><span class="pre">pi</span></tt> member variable. We also keep track of how many results we have received back,
and if that matches the number of tasks sent out, the <tt class="docutils literal"><span class="pre">Master</span></tt> actor considers itself done and
sends the final result to the <tt class="docutils literal"><span class="pre">listener</span></tt>. When done it also invokes the <tt class="docutils literal"><span class="pre">context.stop(self)</span></tt>
method to stop itself <em>and</em> all its supervised actors.
In this case it has one supervised actor, the router, and this in turn has <tt class="docutils literal"><span class="pre">nrOfWorkers</span></tt> supervised actors.
All of them will be stopped automatically as the invocation of any supervisor&#8217;s <tt class="docutils literal"><span class="pre">stop</span></tt> method
will propagate down to all its supervised &#8216;children&#8217;.</p>
<p>Let&#8217;s capture this in code:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">def</span> <span class="n">receive</span> <span class="k">=</span> <span class="o">{</span>
  <span class="k">case</span> <span class="nc">Calculate</span> <span class="k">⇒</span>
    <span class="k">for</span> <span class="o">(</span><span class="n">i</span> <span class="k">←</span> <span class="mi">0</span> <span class="n">until</span> <span class="n">nrOfMessages</span><span class="o">)</span> <span class="n">workerRouter</span> <span class="o">!</span> <span class="nc">Work</span><span class="o">(</span><span class="n">i</span> <span class="o">*</span> <span class="n">nrOfElements</span><span class="o">,</span> <span class="n">nrOfElements</span><span class="o">)</span>
  <span class="k">case</span> <span class="nc">Result</span><span class="o">(</span><span class="n">value</span><span class="o">)</span> <span class="k">⇒</span>
    <span class="n">pi</span> <span class="o">+=</span> <span class="n">value</span>
    <span class="n">nrOfResults</span> <span class="o">+=</span> <span class="mi">1</span>
    <span class="k">if</span> <span class="o">(</span><span class="n">nrOfResults</span> <span class="o">==</span> <span class="n">nrOfMessages</span><span class="o">)</span> <span class="o">{</span>
      <span class="c1">// Send the result to the listener</span>
      <span class="n">listener</span> <span class="o">!</span> <span class="nc">PiApproximation</span><span class="o">(</span><span class="n">pi</span><span class="o">,</span> <span class="n">duration</span> <span class="k">=</span> <span class="o">(</span><span class="nc">System</span><span class="o">.</span><span class="n">currentTimeMillis</span> <span class="o">-</span> <span class="n">start</span><span class="o">).</span><span class="n">millis</span><span class="o">)</span>
      <span class="c1">// Stops this actor and all its supervised children</span>
      <span class="n">context</span><span class="o">.</span><span class="n">stop</span><span class="o">(</span><span class="n">self</span><span class="o">)</span>
    <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
</div>
<div class="section" id="creating-the-result-listener">
<h2>Creating the result listener</h2>
<p>The listener is straightforward. When it receives the <tt class="docutils literal"><span class="pre">PiApproximation</span></tt> from the <tt class="docutils literal"><span class="pre">Master</span></tt> it
prints the result and shuts down the <tt class="docutils literal"><span class="pre">ActorSystem</span></tt>.</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">class</span> <span class="nc">Listener</span> <span class="k">extends</span> <span class="nc">Actor</span> <span class="o">{</span>
  <span class="k">def</span> <span class="n">receive</span> <span class="k">=</span> <span class="o">{</span>
    <span class="k">case</span> <span class="nc">PiApproximation</span><span class="o">(</span><span class="n">pi</span><span class="o">,</span> <span class="n">duration</span><span class="o">)</span> <span class="k">⇒</span>
      <span class="n">println</span><span class="o">(</span><span class="s">&quot;\n\tPi approximation: \t\t%s\n\tCalculation time: \t%s&quot;</span>
        <span class="o">.</span><span class="n">format</span><span class="o">(</span><span class="n">pi</span><span class="o">,</span> <span class="n">duration</span><span class="o">))</span>
      <span class="n">context</span><span class="o">.</span><span class="n">system</span><span class="o">.</span><span class="n">shutdown</span><span class="o">()</span>
  <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
</div>
<div class="section" id="bootstrap-the-calculation">
<h2>Bootstrap the calculation</h2>
<p>Now the only thing that is left to implement is the runner that should bootstrap
and run the calculation for us. We do that by creating an object that we call
<tt class="docutils literal"><span class="pre">Pi</span></tt>, here we can extend the <tt class="docutils literal"><span class="pre">App</span></tt> trait in Scala, which means that we will
be able to run this as an application directly from the command line.</p>
<p>The <tt class="docutils literal"><span class="pre">Pi</span></tt> object is a perfect container module for our actors and messages, so
let&#8217;s put them all there. We also create a method <tt class="docutils literal"><span class="pre">calculate</span></tt> in which we
start up the <tt class="docutils literal"><span class="pre">Master</span></tt> actor and wait for it to finish:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="k">object</span> <span class="nc">Pi</span> <span class="k">extends</span> <span class="nc">App</span> <span class="o">{</span>

  <span class="n">calculate</span><span class="o">(</span><span class="n">nrOfWorkers</span> <span class="k">=</span> <span class="mi">4</span><span class="o">,</span> <span class="n">nrOfElements</span> <span class="k">=</span> <span class="mi">10000</span><span class="o">,</span> <span class="n">nrOfMessages</span> <span class="k">=</span> <span class="mi">10000</span><span class="o">)</span>

  <span class="c1">// actors and messages ...</span>

  <span class="k">def</span> <span class="n">calculate</span><span class="o">(</span><span class="n">nrOfWorkers</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">nrOfElements</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">nrOfMessages</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span> <span class="o">{</span>
    <span class="c1">// Create an Akka system</span>
    <span class="k">val</span> <span class="n">system</span> <span class="k">=</span> <span class="nc">ActorSystem</span><span class="o">(</span><span class="s">&quot;PiSystem&quot;</span><span class="o">)</span>

    <span class="c1">// create the result listener, which will print the result and shutdown the system</span>
    <span class="k">val</span> <span class="n">listener</span> <span class="k">=</span> <span class="n">system</span><span class="o">.</span><span class="n">actorOf</span><span class="o">(</span><span class="nc">Props</span><span class="o">[</span><span class="kt">Listener</span><span class="o">],</span> <span class="n">name</span> <span class="k">=</span> <span class="s">&quot;listener&quot;</span><span class="o">)</span>

    <span class="c1">// create the master</span>
    <span class="k">val</span> <span class="n">master</span> <span class="k">=</span> <span class="n">system</span><span class="o">.</span><span class="n">actorOf</span><span class="o">(</span><span class="nc">Props</span><span class="o">(</span><span class="k">new</span> <span class="nc">Master</span><span class="o">(</span>
      <span class="n">nrOfWorkers</span><span class="o">,</span> <span class="n">nrOfMessages</span><span class="o">,</span> <span class="n">nrOfElements</span><span class="o">,</span> <span class="n">listener</span><span class="o">)),</span>
      <span class="n">name</span> <span class="k">=</span> <span class="s">&quot;master&quot;</span><span class="o">)</span>

    <span class="c1">// start the calculation</span>
    <span class="n">master</span> <span class="o">!</span> <span class="nc">Calculate</span>

  <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
<p>As you can see the <em>calculate</em> method above it creates an <tt class="docutils literal"><span class="pre">ActorSystem</span></tt> and this is the Akka container which
will contain all actors created in that &#8220;context&#8221;. An example of how to create actors in the container
is the <em>&#8216;system.actorOf(...)&#8217;</em> line in the calculate method. In this case we create two top level actors.
If you instead where in an actor context, i.e. inside an actor creating other actors, you should use
<em>context.actorOf(...)</em>. This is illustrated in the Master code above.</p>
<p>That&#8217;s it. Now we are done.</p>
<p>But before we package it up and run it, let&#8217;s take a look at the full code now,
with package declaration, imports and all:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="cm">/**</span>
<span class="cm"> * Copyright (C) 2009-2012 Typesafe Inc. &lt;http://www.typesafe.com&gt;</span>
<span class="cm"> */</span>
<span class="k">package</span> <span class="nn">akka.tutorial.first.scala</span>

<span class="k">import</span> <span class="nn">akka.actor._</span>
<span class="k">import</span> <span class="nn">akka.routing.RoundRobinRouter</span>
<span class="k">import</span> <span class="nn">akka.util.Duration</span>
<span class="k">import</span> <span class="nn">akka.util.duration._</span>

<span class="k">object</span> <span class="nc">Pi</span> <span class="k">extends</span> <span class="nc">App</span> <span class="o">{</span>

  <span class="n">calculate</span><span class="o">(</span><span class="n">nrOfWorkers</span> <span class="k">=</span> <span class="mi">4</span><span class="o">,</span> <span class="n">nrOfElements</span> <span class="k">=</span> <span class="mi">10000</span><span class="o">,</span> <span class="n">nrOfMessages</span> <span class="k">=</span> <span class="mi">10000</span><span class="o">)</span>

  <span class="k">sealed</span> <span class="k">trait</span> <span class="nc">PiMessage</span>
  <span class="k">case</span> <span class="k">object</span> <span class="nc">Calculate</span> <span class="k">extends</span> <span class="nc">PiMessage</span>
  <span class="k">case</span> <span class="k">class</span> <span class="nc">Work</span><span class="o">(</span><span class="n">start</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">nrOfElements</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span> <span class="k">extends</span> <span class="nc">PiMessage</span>
  <span class="k">case</span> <span class="k">class</span> <span class="nc">Result</span><span class="o">(</span><span class="n">value</span><span class="k">:</span> <span class="kt">Double</span><span class="o">)</span> <span class="k">extends</span> <span class="nc">PiMessage</span>
  <span class="k">case</span> <span class="k">class</span> <span class="nc">PiApproximation</span><span class="o">(</span><span class="n">pi</span><span class="k">:</span> <span class="kt">Double</span><span class="o">,</span> <span class="n">duration</span><span class="k">:</span> <span class="kt">Duration</span><span class="o">)</span>

  <span class="k">class</span> <span class="nc">Worker</span> <span class="k">extends</span> <span class="nc">Actor</span> <span class="o">{</span>

    <span class="k">def</span> <span class="n">calculatePiFor</span><span class="o">(</span><span class="n">start</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">nrOfElements</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span><span class="k">:</span> <span class="kt">Double</span> <span class="o">=</span> <span class="o">{</span>
      <span class="k">var</span> <span class="n">acc</span> <span class="k">=</span> <span class="mf">0.0</span>
      <span class="k">for</span> <span class="o">(</span><span class="n">i</span> <span class="k">←</span> <span class="n">start</span> <span class="n">until</span> <span class="o">(</span><span class="n">start</span> <span class="o">+</span> <span class="n">nrOfElements</span><span class="o">))</span>
        <span class="n">acc</span> <span class="o">+=</span> <span class="mf">4.0</span> <span class="o">*</span> <span class="o">(</span><span class="mi">1</span> <span class="o">-</span> <span class="o">(</span><span class="n">i</span> <span class="o">%</span> <span class="mi">2</span><span class="o">)</span> <span class="o">*</span> <span class="mi">2</span><span class="o">)</span> <span class="o">/</span> <span class="o">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">i</span> <span class="o">+</span> <span class="mi">1</span><span class="o">)</span>
      <span class="n">acc</span>
    <span class="o">}</span>

    <span class="k">def</span> <span class="n">receive</span> <span class="k">=</span> <span class="o">{</span>
      <span class="k">case</span> <span class="nc">Work</span><span class="o">(</span><span class="n">start</span><span class="o">,</span> <span class="n">nrOfElements</span><span class="o">)</span> <span class="k">⇒</span>
        <span class="n">sender</span> <span class="o">!</span> <span class="nc">Result</span><span class="o">(</span><span class="n">calculatePiFor</span><span class="o">(</span><span class="n">start</span><span class="o">,</span> <span class="n">nrOfElements</span><span class="o">))</span> <span class="c1">// perform the work</span>
    <span class="o">}</span>
  <span class="o">}</span>

  <span class="k">class</span> <span class="nc">Master</span><span class="o">(</span><span class="n">nrOfWorkers</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">nrOfMessages</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">nrOfElements</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">listener</span><span class="k">:</span> <span class="kt">ActorRef</span><span class="o">)</span>
    <span class="k">extends</span> <span class="nc">Actor</span> <span class="o">{</span>

    <span class="k">var</span> <span class="n">pi</span><span class="k">:</span> <span class="kt">Double</span> <span class="o">=</span> <span class="k">_</span>
    <span class="k">var</span> <span class="n">nrOfResults</span><span class="k">:</span> <span class="kt">Int</span> <span class="o">=</span> <span class="k">_</span>
    <span class="k">val</span> <span class="n">start</span><span class="k">:</span> <span class="kt">Long</span> <span class="o">=</span> <span class="nc">System</span><span class="o">.</span><span class="n">currentTimeMillis</span>

    <span class="k">val</span> <span class="n">workerRouter</span> <span class="k">=</span> <span class="n">context</span><span class="o">.</span><span class="n">actorOf</span><span class="o">(</span>
      <span class="nc">Props</span><span class="o">[</span><span class="kt">Worker</span><span class="o">].</span><span class="n">withRouter</span><span class="o">(</span><span class="nc">RoundRobinRouter</span><span class="o">(</span><span class="n">nrOfWorkers</span><span class="o">)),</span> <span class="n">name</span> <span class="k">=</span> <span class="s">&quot;workerRouter&quot;</span><span class="o">)</span>

    <span class="k">def</span> <span class="n">receive</span> <span class="k">=</span> <span class="o">{</span>
      <span class="k">case</span> <span class="nc">Calculate</span> <span class="k">⇒</span>
        <span class="k">for</span> <span class="o">(</span><span class="n">i</span> <span class="k">←</span> <span class="mi">0</span> <span class="n">until</span> <span class="n">nrOfMessages</span><span class="o">)</span> <span class="n">workerRouter</span> <span class="o">!</span> <span class="nc">Work</span><span class="o">(</span><span class="n">i</span> <span class="o">*</span> <span class="n">nrOfElements</span><span class="o">,</span> <span class="n">nrOfElements</span><span class="o">)</span>
      <span class="k">case</span> <span class="nc">Result</span><span class="o">(</span><span class="n">value</span><span class="o">)</span> <span class="k">⇒</span>
        <span class="n">pi</span> <span class="o">+=</span> <span class="n">value</span>
        <span class="n">nrOfResults</span> <span class="o">+=</span> <span class="mi">1</span>
        <span class="k">if</span> <span class="o">(</span><span class="n">nrOfResults</span> <span class="o">==</span> <span class="n">nrOfMessages</span><span class="o">)</span> <span class="o">{</span>
          <span class="c1">// Send the result to the listener</span>
          <span class="n">listener</span> <span class="o">!</span> <span class="nc">PiApproximation</span><span class="o">(</span><span class="n">pi</span><span class="o">,</span> <span class="n">duration</span> <span class="k">=</span> <span class="o">(</span><span class="nc">System</span><span class="o">.</span><span class="n">currentTimeMillis</span> <span class="o">-</span> <span class="n">start</span><span class="o">).</span><span class="n">millis</span><span class="o">)</span>
          <span class="c1">// Stops this actor and all its supervised children</span>
          <span class="n">context</span><span class="o">.</span><span class="n">stop</span><span class="o">(</span><span class="n">self</span><span class="o">)</span>
        <span class="o">}</span>
    <span class="o">}</span>

  <span class="o">}</span>

  <span class="k">class</span> <span class="nc">Listener</span> <span class="k">extends</span> <span class="nc">Actor</span> <span class="o">{</span>
    <span class="k">def</span> <span class="n">receive</span> <span class="k">=</span> <span class="o">{</span>
      <span class="k">case</span> <span class="nc">PiApproximation</span><span class="o">(</span><span class="n">pi</span><span class="o">,</span> <span class="n">duration</span><span class="o">)</span> <span class="k">⇒</span>
        <span class="n">println</span><span class="o">(</span><span class="s">&quot;\n\tPi approximation: \t\t%s\n\tCalculation time: \t%s&quot;</span>
          <span class="o">.</span><span class="n">format</span><span class="o">(</span><span class="n">pi</span><span class="o">,</span> <span class="n">duration</span><span class="o">))</span>
        <span class="n">context</span><span class="o">.</span><span class="n">system</span><span class="o">.</span><span class="n">shutdown</span><span class="o">()</span>
    <span class="o">}</span>
  <span class="o">}</span>


  <span class="k">def</span> <span class="n">calculate</span><span class="o">(</span><span class="n">nrOfWorkers</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">nrOfElements</span><span class="k">:</span> <span class="kt">Int</span><span class="o">,</span> <span class="n">nrOfMessages</span><span class="k">:</span> <span class="kt">Int</span><span class="o">)</span> <span class="o">{</span>
    <span class="c1">// Create an Akka system</span>
    <span class="k">val</span> <span class="n">system</span> <span class="k">=</span> <span class="nc">ActorSystem</span><span class="o">(</span><span class="s">&quot;PiSystem&quot;</span><span class="o">)</span>

    <span class="c1">// create the result listener, which will print the result and shutdown the system</span>
    <span class="k">val</span> <span class="n">listener</span> <span class="k">=</span> <span class="n">system</span><span class="o">.</span><span class="n">actorOf</span><span class="o">(</span><span class="nc">Props</span><span class="o">[</span><span class="kt">Listener</span><span class="o">],</span> <span class="n">name</span> <span class="k">=</span> <span class="s">&quot;listener&quot;</span><span class="o">)</span>

    <span class="c1">// create the master</span>
    <span class="k">val</span> <span class="n">master</span> <span class="k">=</span> <span class="n">system</span><span class="o">.</span><span class="n">actorOf</span><span class="o">(</span><span class="nc">Props</span><span class="o">(</span><span class="k">new</span> <span class="nc">Master</span><span class="o">(</span>
      <span class="n">nrOfWorkers</span><span class="o">,</span> <span class="n">nrOfMessages</span><span class="o">,</span> <span class="n">nrOfElements</span><span class="o">,</span> <span class="n">listener</span><span class="o">)),</span>
      <span class="n">name</span> <span class="k">=</span> <span class="s">&quot;master&quot;</span><span class="o">)</span>

    <span class="c1">// start the calculation</span>
    <span class="n">master</span> <span class="o">!</span> <span class="nc">Calculate</span>

  <span class="o">}</span>
<span class="o">}</span>
</pre></div>
</div>
</div>
<div class="section" id="run-it-as-a-command-line-application">
<h2>Run it as a command line application</h2>
<p>If you have not typed in (or copied) the code for the tutorial as in
<tt class="docutils literal"><span class="pre">$AKKA_HOME/akka-tutorials/akka-tutorial-first/src/main/scala/akka/tutorial/first/scala/Pi.scala</span></tt> then now is the time.
When that&#8217;s done open up a shell and step in to the Akka distribution (<tt class="docutils literal"><span class="pre">cd</span> <span class="pre">$AKKA_HOME</span></tt>).</p>
<p>First we need to compile the source file. That is done with Scala&#8217;s compiler
<tt class="docutils literal"><span class="pre">scalac</span></tt>. Our application depends on the <tt class="docutils literal"><span class="pre">akka-actor-2.0.1.jar</span></tt> JAR
file, so let&#8217;s add that to the compiler classpath when we compile the source.</p>
<p>On Linux/Unix/Mac systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">$</span> <span class="n">scalac</span> <span class="o">-</span><span class="n">cp</span> <span class="n">lib</span><span class="o">/</span><span class="n">akka</span><span class="o">/</span><span class="n">akka</span><span class="o">-</span><span class="n">actor</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mf">1.</span><span class="n">jar</span> <span class="nc">Pi</span><span class="o">.</span><span class="n">scala</span>
</pre></div>
</div>
<p>On Windows systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span><span class="o">&gt;</span> <span class="n">scalac</span> <span class="o">-</span><span class="n">cp</span> <span class="n">lib</span><span class="o">\</span><span class="n">akka</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="n">actor</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mf">1.</span><span class="n">jar</span> <span class="nc">Pi</span><span class="o">.</span><span class="n">scala</span>
</pre></div>
</div>
<p>When we have compiled the source file we are ready to run the application. This
is done with <tt class="docutils literal"><span class="pre">java</span></tt> but yet again we need to add the
<tt class="docutils literal"><span class="pre">akka-actor-2.0.1.jar</span></tt> JAR file to the classpath, and this time we also
need to add the Scala runtime library <tt class="docutils literal"><span class="pre">scala-library.jar</span></tt> and the classes we
compiled ourselves.</p>
<p>On Linux/Unix/Mac systems:</p>
<div class="highlight-scala"><pre>$ java \
    -cp lib/scala-library.jar:lib/akka/akka-actor-2.0.1.jar:. \
    akka.tutorial.first.scala.Pi

Pi approximation:   3.1415926435897883
Calculation time:   359 millis</pre>
</div>
<p>On Windows systems:</p>
<div class="highlight-scala"><pre>C:\Users\jboner\src\akka\akka-2.0.1&gt; java \
    -cp lib/scala-library.jar;lib\akka\akka-actor-2.0.1.jar;. \
    akka.tutorial.first.scala.Pi

Pi approximation:   3.1415926435897883
Calculation time:   359 millis</pre>
</div>
<p>Yippee! It is working.</p>
</div>
<div class="section" id="run-it-inside-sbt">
<h2>Run it inside SBT</h2>
<p>If you used SBT, then you can run the application directly inside SBT. First you
need to compile the project.</p>
<p>On Linux/Unix/Mac systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">$</span> <span class="n">sbt</span>
<span class="o">&gt;</span> <span class="n">compile</span>
<span class="o">...</span>
</pre></div>
</div>
<p>On Windows systems:</p>
<div class="highlight-scala"><div class="highlight"><pre><span class="n">C</span><span class="o">:\</span><span class="nc">Users</span><span class="o">\</span><span class="n">jboner</span><span class="o">\</span><span class="n">src</span><span class="o">\</span><span class="n">akka</span><span class="o">\</span><span class="n">akka</span><span class="o">-</span><span class="mf">2.0</span><span class="o">.</span><span class="mi">1</span><span class="o">&gt;</span> <span class="n">sbt</span>
<span class="o">&gt;</span> <span class="n">compile</span>
<span class="o">...</span>
</pre></div>
</div>
<p>When this in done we can run our application directly inside SBT:</p>
<div class="highlight-scala"><pre>&gt; run
...
Pi approximation:   3.1415926435897883
Calculation time:   359 millis</pre>
</div>
<p>Yippee! It is working.</p>
</div>
<div class="section" id="overriding-configuration-externally-optional">
<h2>Overriding Configuration Externally (Optional)</h2>
<p>The sample project includes an <tt class="docutils literal"><span class="pre">application.conf</span></tt> file in the resources directory:</p>
<div class="highlight-scala"><pre>akka.actor.deployment {
  /master/workerRouter {
    # Uncomment the following two lines to change the calculation to use 10 workers instead of 4:
    #router = round-robin
    #nr-of-instances = 10
  }
}
</pre>
</div>
<p>If you uncomment the two lines, you should see a change in performance,
hopefully for the better (you might want to increase the number of messages in
the code to prolong the time the application runs). It should be noted that
overriding only works if a router type is given, so just uncommenting
<tt class="docutils literal"><span class="pre">nr-of-instances</span></tt> does not work; see <a class="reference internal" href="../scala/routing.html#routing-scala"><em>Routing (Scala)</em></a> for more details.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Make sure that your <tt class="docutils literal"><span class="pre">application.conf</span></tt> is on the class path when you run
the application. If running from inside SBT that should already be the case,
otherwise you need to add the directory containing this file to the JVM’s
<tt class="docutils literal"><span class="pre">-classpath</span></tt> option.</p>
</div>
</div>
<div class="section" id="conclusion">
<h2>Conclusion</h2>
<p>We have learned how to create our first Akka project using Akka&#8217;s actors to
speed up a computation-intensive problem by scaling out on multi-core processors
(also known as scaling up). We have also learned to compile and run an Akka
project using either the tools on the command line or the SBT build system.</p>
<p>If you have a multi-core machine then I encourage you to try out different
number of workers (number of working actors) by tweaking the <tt class="docutils literal"><span class="pre">nrOfWorkers</span></tt>
variable to for example; 2, 4, 6, 8 etc. to see performance improvement by
scaling up.</p>
<p>Happy hakking.</p>
</div>
</div>


          </div>
          <div class="span3"><p class="contents-title">Contents</p>
              <div id="scroller-anchor">
                <div id="scroller">
                  <div id="toc"></div>
                </div>
              </div></div>
        </div>
      </div>
    </div>
  </div>
  <div class="footer">
  <div class="container">
    <ul>
      <li><h5>Akka</h5></li>
      <li><a href="http://akka.io/docs">Documentation</a></li>
      <li><a href="http://akka.io/downloads">Downloads</a></li>
    </ul>
    <ul>
      <li><h5>Contribute</h5></li>
      <li><a href="http://github.com/akka/akka">Source Code</a></li>
      <li><a href="http://groups.google.com/group/akka-user">Mailing List</a></li>      
      <li><a href="http://www.assembla.com/spaces/akka/tickets">Report a Bug</a></li>      
    </ul>
    <ul>
      <li><h5>Company</h5></li>
      <li><a href="http://typesafe.com/products/typesafe-subscription">Commercial Support</a></li>
      <li><a href="http://akka.io/team">Team</a></li>
      <li><a href="mailto:info@typesafe.com">Contact</a></li>
    </ul>
    <ul>
      <li><img src="../_static/watermark.png" align="center"/></li>
    </ul>
  </div>
  <div class="container copyright">
    <p style="float: left;">
      © 2012 <a href="http://typesafe.com/">Typesafe Inc.</a> <span class="license">Akka is Open Source and available under the Apache 2 License.</span>
    </p>
    <p style="float: right; font-size: 12px;">
      Last updated: Apr 13, 2012
    </p>          
  </div>
</div>
<script type="text/javascript">
  $('#toc').toc();
</script>
  

  </body>
</html>